package com.xhh.nphm.gaoxin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhh.nphm.common.annotation.RepeatSubmit;
import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.common.utils.ShiroUtils;
import com.xhh.nphm.common.utils.StringUtils;
import com.xhh.nphm.gaoxin.entity.*;
import com.xhh.nphm.gaoxin.service.*;
import com.xhh.nphm.system.domain.*;
import com.xhh.nphm.system.service.*;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/gaoxin/system/mTelRcd")
public class GxqTelGetDealController extends BaseController {
    private String prefix = "system/gaoxin/mTelRcd";
    @Autowired
    private GxqITelGetDealService gxqTelGetDealService;
    @Autowired
    private GxqIWorkFormBaseService gxqWorkFormBaseService;
    @Autowired
    private GxqIWfSendBackService gxqWfSendBackService;
    @Autowired
    private ISensitiveInfoService sensitiveService;
    @Autowired
    private GxqTGetDealLogService gxqTelGetDealLogService;
    @Autowired
    private GxqITelGetDealTempService gxqTelGetDealTempService;

    /**
     * 查询来电记录草稿列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @RequestMapping("listDraft")
    public TableDataInfo listDraft(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        GxqTtelGetDealEty.setLiftWorkNo(user.getUserNo());
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.listDraft(GxqTtelGetDealEty);
        //隐藏电话号码 0不隐藏 5隐藏
        for(GxqTtelGetDealEty t:list){
            if(t.getHideTel()==5){
                t.setFromTelA(t.getFromTelA().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
            }
        }
        return getDataTable(list);
    }

    @RequestMapping("listPending")
    public TableDataInfo listPending(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.listPending(GxqTtelGetDealEty);
        return getDataTable(list);
    }

    /**
     * 我处理的来电列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @RequestMapping("listHandle")
    public TableDataInfo listHandle(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        GxqTtelGetDealEty.setLiftWorkNo(user.getUserNo());
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.listHandle(GxqTtelGetDealEty);
        return getDataTable(list);
    }

    /**
     * 坐席办结来电列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @RequestMapping("listFinish")
    public TableDataInfo listFinish(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.listFinish(GxqTtelGetDealEty);
        return getDataTable(list);
    }

    /**
     * 已派工单来电列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @RequestMapping("listWork")
    public TableDataInfo listWork(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.listWork(GxqTtelGetDealEty);
        return getDataTable(list);
    }

    /**
     * 查询我接听的来电列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @RequestMapping("myGetList")
    public TableDataInfo myGetList(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        GxqTtelGetDealEty.setLiftWorkNo(user.getUserNo());
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.myGetList(GxqTtelGetDealEty);
        //隐藏电话号码 0不隐藏 5隐藏
        for(GxqTtelGetDealEty t:list){
            if(t.getHideTel()==5){
                t.setFromTelA(t.getFromTelA().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
            }
        }
        return getDataTable(list);
    }

    /**
     * 来电记录草稿列表页面
     *
     * @return
     */
    @RequiresPermissions("mTelRcd:listDraft:view")
    @GetMapping("/listDraft")
    public ModelAndView listDraftPage() {
        return new ModelAndView(prefix + "/listDraft");
    }

    /**
     * 我接听的来电  坐席办结来电  已派工单来电  修改页面
     *
     * @return
     */
    @RequiresPermissions("mTelRcd:processEdit:view")
    @GetMapping("/processEdit/{pkId}")
    public ModelAndView processEdit(@PathVariable("pkId") Integer pkId, ModelMap map) {

        GxqTtelGetDealEty GxqTtelGetDealEty = gxqTelGetDealService.selectByPkId(pkId);
        map.put("problem", gxqTelGetDealService.problem(GxqTtelGetDealEty.getFromTelA()));
        map.put("history", gxqTelGetDealService.countHistory(GxqTtelGetDealEty.getFromTelA()));
        map.put("noLike", gxqTelGetDealService.noLike(GxqTtelGetDealEty.getFromTelA()));
        map.put("telGetDeal", GxqTtelGetDealEty);
        map.put("sensitive", sensitiveService.findOneByPkId(GxqTtelGetDealEty.getPkId()));
        String operator = "";
        if (StringUtils.isNotEmpty(GxqTtelGetDealEty.getYworkNo())) {
            operator = "[" + GxqTtelGetDealEty.getYworkNo() + "]";
        }
        operator = operator + GxqTtelGetDealEty.getYworker();
        map.put("operator", operator);
        return new ModelAndView(prefix + "/processEdit");
    }

    /**
     * 我接听的来电详情
     *
     * @return
     */
    @GetMapping("/myGetDetail/{pkId}")
    public ModelAndView myTelDetail(@PathVariable("pkId") Integer pkId, ModelMap map) {
        GxqTtelGetDealEty GxqTtelGetDealEty = gxqTelGetDealService.selectByPkId(pkId);
        String operator = "";
        if (StringUtils.isNotEmpty(GxqTtelGetDealEty.getYworkNo())) {
            operator = "[" + GxqTtelGetDealEty.getYworkNo() + "]";
        }
        if (StringUtils.isNotEmpty(GxqTtelGetDealEty.getYworker())) {
            operator += GxqTtelGetDealEty.getYworker();
        }
        //隐藏电话号码 0不隐藏 5隐藏
        System.out.println("是否隐藏"+GxqTtelGetDealEty.getHideTel());
        if(GxqTtelGetDealEty.getHideTel()==5){
            GxqTtelGetDealEty.setFromTelA(GxqTtelGetDealEty.getFromTelA().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
        }
        map.put("telGetDeal", GxqTtelGetDealEty);
        map.put("operator", operator);
        map.put("problem", gxqTelGetDealService.problem(GxqTtelGetDealEty.getFromTelA()));
        map.put("history", gxqTelGetDealService.countHistory(GxqTtelGetDealEty.getFromTelA()));
        map.put("noLike", gxqTelGetDealService.noLike(GxqTtelGetDealEty.getFromTelA()));
        return new ModelAndView(prefix + "/myGetDetail");
    }


    /**
     * 待处理来电记录列表页面
     *
     * @return
     */
    @RequiresPermissions("mTelRcd:listWait:view")
    @GetMapping("/listWait")
    public ModelAndView listWait() {
        return new ModelAndView(prefix + "/listWait");
    }

    /**
     * 查询待处理来电记录列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @RequestMapping("listWait")
    public TableDataInfo listWait(GxqTtelGetDealEty GxqTtelGetDealEty) {
        SysUser user = ShiroUtils.getSysUser();
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.listWait(user.getUserNo(),user.getCarrierType(), GxqTtelGetDealEty);
//        if(!"304".equals(user.getDeptId()) && user.getDeptId().toString().length()==3 || user.getDeptId().toString().length()/3==2 || user.getDeptId().toString().length()/3==3){
//            //隐藏电话号码 0不隐藏 5隐藏
//            for(GxqTtelGetDealEty t:list){
//                if(t.getHideTel()==5){
//                    t.setFromTelA(t.getFromTelA().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
//                }
//            }
//        }
        return getDataTable(list);
    }

    /**
     * 随机获取页面
     *
     * @return
     */
    @GetMapping("/countListWait")
    public ModelAndView countListWait(ModelMap map) {
        // 查询 未获取待处理来电记录条数
        map.put("num",gxqTelGetDealService.countListWait(getSysUser().getCarrierType()));
        return new ModelAndView(prefix + "/countListWait");
    }

    /**
     * 查询 未获取待处理来电记录条数
     *
     * @return
     */
    @GetMapping("/count")
    public int countWait() {
        // 查询 未获取待处理来电记录条数
        return gxqTelGetDealService.countListWait(getSysUser().getCarrierType());
    }

    /**
     * 未获取列表页面
     * @return
     */
    @GetMapping("/noGetListWait")
    public ModelAndView noGetListWait() {
        return new ModelAndView(prefix + "/noGetListWait");
    }

    /**
     * 未获取待处理来电记录 列表数据
     *
     * @param
     * @return
     */
    @PostMapping("noGetListWait")
    public TableDataInfo noGetListWait(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.noGetListWait(getSysUser().getCarrierType(),GxqTtelGetDealEty);;
        return getDataTable(list);
    }

    /**
     * 重录入列表页面
     * @return
     */
    @GetMapping("/reenterList")
    public ModelAndView reenterList() {
        return new ModelAndView(prefix + "/reenterList");
    }

    /**
     * 紧急件列表页面
     * @return
     */
    @GetMapping("/urgentList")
    public ModelAndView urgentList() {
        return new ModelAndView(prefix + "/urgentList");
    }

    /**
     * 待办件列表页面
     * @return
     */
    @GetMapping("/getWaitList")
    public ModelAndView getWaitList() {
        return new ModelAndView(prefix + "/getWaitList");
    }

    /**
     * 重录件列表数据
     *
     * @param
     * @return
     */
    @RequestMapping("reenterList")
    public TableDataInfo reenterList(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.reenterList(getSysUser().getCarrierType(),GxqTtelGetDealEty);
        return getDataTable(list);
    }

    /**
     * 紧急件列表数据
     *
     * @param
     * @return
     */
    @RequestMapping("urgentListJson")
    public TableDataInfo urgentListJson(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.urgentList(getSysUser().getCarrierType(), GxqTtelGetDealEty);
        return getDataTable(list);
    }

    @RequiresPermissions("mTelRcd:add:view")
    @GetMapping("/add")
    public ModelAndView telAdd(ModelMap map) {
        GxqTtelGetDealEty GxqTtelGetDealEty = new GxqTtelGetDealEty();
        GxqTtelGetDealEty.setFromTime(new Date());
        /**
         * 设置默认 接听人
         */
        SysUser user = ShiroUtils.getSysUser();
        //设置姓名
        GxqTtelGetDealEty.setLiftWorker(user.getUserName());
        //设置用户编号
        GxqTtelGetDealEty.setLiftWorkNo(user.getUserNo());
        //设置分机号
        GxqTtelGetDealEty.setLiftTelNo(user.getTklTNo());
        map.put("telGetDeal", GxqTtelGetDealEty);
        map.put("problem", gxqTelGetDealService.problem(GxqTtelGetDealEty.getFromTelA()));
        map.put("history", gxqTelGetDealService.countHistory(GxqTtelGetDealEty.getFromTelA()));
        map.put("noLike", gxqTelGetDealService.noLike(GxqTtelGetDealEty.getFromTelA()));
        return new ModelAndView(prefix + "/add");
    }

    /**
     * 接听页面
     *
     * @param map
     * @return
     */
    @GetMapping("/telGetNew")
    public ModelAndView teladd(ModelMap map, HttpServletRequest request) {
        GxqTtelGetDealEty telGetDeal = new GxqTtelGetDealEty();
        telGetDeal.setFromTelA(request.getParameter("callin"));
        telGetDeal.setCallId(request.getParameter("callid"));
        telGetDeal.setTapeId(request.getParameter("tapeid"));
        telGetDeal.setFromTelC(request.getParameter("calledid"));
        telGetDeal.setFromTime(new Date());
        SysUser user = ShiroUtils.getSysUser();
        //设置姓名
        telGetDeal.setLiftWorker(user.getUserName());
        //设置用户编号
        telGetDeal.setLiftWorkNo(user.getUserNo());
        //设置分机号
        telGetDeal.setLiftTelNo(user.getTklTNo());
        map.put("telGetDeal", telGetDeal);
        map.put("problem", gxqTelGetDealService.problem(telGetDeal.getFromTelA()));
        map.put("history", gxqTelGetDealService.countHistory(telGetDeal.getFromTelA()));
        map.put("noLike", gxqTelGetDealService.noLike(telGetDeal.getFromTelA()));
        return new ModelAndView(prefix + "/telGetNew");
    }

    /**
     * 新增来电记录草稿
     *
     * @param
     * @return
     */
    @RequestMapping("save")
    @RepeatSubmit
    public AjaxResult save(GxqTtelGetDealEty telGetDealEty, EventAddress eventAddress, SensitiveInfo sensitiveInfo, Integer sensitiveId) {
        // 封装操作记录信息
        SysUser user = ShiroUtils.getSysUser();
        TworkFormLogEty tworkFormLogEty = telGetDealEty.getTworkFormLogEty();
        tworkFormLogEty.setOpDeptNo(String.valueOf(user.getDeptId()));
        tworkFormLogEty.setOpDeptName(user.getDeptName());
        tworkFormLogEty.setOpUserNo(user.getUserNo());
        tworkFormLogEty.setOpUserName(user.getUserName());
        tworkFormLogEty.setOpAction("新增来电记录");
        tworkFormLogEty.setOpBtnId(-1);

        //设置默认地区
//        telGetDealEty.setFmAreaNo("5101");
//        telGetDealEty.setFmAreaName("成都市");
        //根据事发地址，解析出地区
//        List<Map> area = commonService.findArea();
//        for (Map map : area) {
//            Object text = map.get("CSText");
//            if (text.equals(telGetDealEty.getFmAreaName())) {
//                Object value = map.get("CSValue");
//                telGetDealEty.setFmAreaNo((String) value);
//                break;
//            }
//        }
        //更新敏感信息
        sensitiveInfo.setId(sensitiveId);
        telGetDealEty.setRepeatNos(telGetDealEty.getFromTelA());
        int insert = gxqTelGetDealService.insert(telGetDealEty, eventAddress, sensitiveInfo);
        //封装来电记录 日志 操作信息
        if (insert > 0) {
            GxqTGetDealLog GxqTGetDealLog = new GxqTGetDealLog();
            GxqTGetDealLog.setOpDeptNo(String.valueOf(user.getDeptId()));
            GxqTGetDealLog.setOpDeptName(user.getDeptName());
            GxqTGetDealLog.setOpUserNo(user.getUserNo());
            GxqTGetDealLog.setOpUserName(user.getUserName());
            GxqTGetDealLog.setOpAction("新增来电记录");
            GxqTGetDealLog.setTelId(telGetDealEty.getPkId());
            gxqTelGetDealLogService.save(GxqTGetDealLog);
        }
        AjaxResult ajaxResult = AjaxResult.error();
        if (insert > 0) {
            ajaxResult = AjaxResult.success("新增成功", telGetDealEty);
        }
        return ajaxResult;
    }

    /**
     * 新增来电记录草稿
     *
     * @param
     * @return
     */
    @RequestMapping("add")
    @RepeatSubmit
    public AjaxResult add(GxqTtelGetDealEty telGetDeal) {
        // 封装操作记录信息
        SysUser user = ShiroUtils.getSysUser();
        TworkFormLogEty tworkFormLogEty = telGetDeal.getTworkFormLogEty();
        tworkFormLogEty.setOpDeptNo(String.valueOf(user.getDeptId()));
        tworkFormLogEty.setOpDeptName(user.getDeptName());
        tworkFormLogEty.setOpUserNo(user.getUserNo());
        tworkFormLogEty.setOpUserName(user.getUserName());
        tworkFormLogEty.setOpAction("新增来电记录");
        tworkFormLogEty.setOpBtnId(-1);
        return toAjax(gxqTelGetDealService.insert(telGetDeal));
    }

    /**
     * 退回坐席 暂存 回电解答 作废 创建工单 页面
     *
     * @param pkId
     * @param map
     * @return
     */
    @RequiresPermissions("mTelRcd:edit:view")
    @GetMapping("/{type}/edit/{pkId}")
    public ModelAndView edit(@PathVariable("pkId") Integer  pkId,@PathVariable("type") String type, ModelMap map) {
        GxqTtelGetDealEty GxqTtelGetDealEty = gxqTelGetDealService.selectByPkId(pkId);
        map.put("telGetDeal", GxqTtelGetDealEty);
        map.put("problem", gxqTelGetDealService.problem(GxqTtelGetDealEty.getFromTelA()));
        map.put("history", gxqTelGetDealService.countHistory(GxqTtelGetDealEty.getFromTelA()));
        map.put("noLike", gxqTelGetDealService.noLike(GxqTtelGetDealEty.getFromTelA()));
        map.put("sensitive", sensitiveService.findOneByPkId(GxqTtelGetDealEty.getPkId()));
        if("listDraft".equals(type)){
            return new ModelAndView(prefix + "/listDraftEdit");
        }else {
            return new ModelAndView(prefix + "/edit");
        }

    }

    /**
     * 修改来电记录草稿
     * 退回坐席 暂存 回电解答 作废
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @PostMapping("edit")
    @RepeatSubmit
    public AjaxResult edit(GxqTtelGetDealEty GxqTtelGetDealEty, Integer sensitiveId) {
        // 封装操作记录信息
        SysUser user = ShiroUtils.getSysUser();
        TworkFormLogEty tworkFormLogEty = GxqTtelGetDealEty.getTworkFormLogEty();
        tworkFormLogEty.setOpDeptNo(String.valueOf(user.getDeptId()));
        tworkFormLogEty.setOpDeptName(user.getDeptName());
        tworkFormLogEty.setOpUserNo(user.getUserNo());
        tworkFormLogEty.setOpUserName(user.getUserName());
        tworkFormLogEty.setOpAction("修改来电记录");
        tworkFormLogEty.setOpBtnId(-1);
//        List<Map> area = commonService.findArea();
//        for (Map map : area) {
//            Object text = map.get("CSText");
//            if (text.equals(GxqTtelGetDealEty.getFmAreaName())) {
//                Object value = map.get("CSValue");
//                GxqTtelGetDealEty.setFmAreaNo((String) value);
//                break;
//            }
//        }
        //如果来电记录草稿没有敏感信息
        if (sensitiveService.findOneByPkId(GxqTtelGetDealEty.getPkId()) == null) {
            //如果有敏感信息记录
            if (sensitiveService.getById(sensitiveId) != null) {
                SensitiveInfo sensitiveInfo = new SensitiveInfo();
                sensitiveInfo.setId(sensitiveId);
                //绑定来电记录草稿pkId
                sensitiveInfo.setTelGetDealPk(GxqTtelGetDealEty.getPkId());
                sensitiveService.updateById(sensitiveInfo);
            }
        }
        //封装来电记录操作信息
        GxqTGetDealLog GxqTGetDealLog = new GxqTGetDealLog();
        GxqTGetDealLog.setTelId(GxqTtelGetDealEty.getPkId());
        GxqTGetDealLog.setOpDeptNo(String.valueOf(user.getDeptId()));
        GxqTGetDealLog.setOpDeptName(user.getDeptName());
        GxqTGetDealLog.setOpUserNo(user.getUserNo());
        GxqTGetDealLog.setOpUserName(user.getUserName());
        GxqTGetDealLog.setOpAction("修改来电记录");
        //新增 修改来电记录操作信息
        gxqTelGetDealLogService.save(GxqTGetDealLog);

        //电话记录修改人相关信息 退回坐席 暂存 回电解答 作废
        //TODO: system/test/telhandle.html 作废132？？？
        int stateFlag = GxqTtelGetDealEty.getStateFlag();
        if (stateFlag == 121 || stateFlag == 122 || stateFlag == 136 || stateFlag == 135) {
            GxqTtelGetDealEty.setYworker(user.getUserName());
            GxqTtelGetDealEty.setYeditTime(new Date());
            GxqTtelGetDealEty.setYworkNo(user.getUserNo());
        }
        AjaxResult ajaxResult = toAjax(gxqTelGetDealService.update(GxqTtelGetDealEty), "编辑成功");
        ajaxResult.put("data", GxqTtelGetDealEty);
        return ajaxResult;
    }

    /**
     * 我接听的来电列表页面
     *
     * @return
     */
    @RequiresPermissions("mTelRcd:myGetList:view")
    @GetMapping("/myGetList")
    public ModelAndView myGetListPage() {
        return new ModelAndView(prefix + "/myGetList");
    }

    /**
     * 待处理来电记录 处理页面 退回待坐席处理 编辑页面
     * 我处理的来电 编辑页面
     *
     * @param pkId
     * @param map
     * @return
     */
    @RequiresPermissions("mTelRcd:process:view")
    @GetMapping("/process/{pkId}")
    public ModelAndView process(@PathVariable("pkId") Integer pkId, ModelMap map) {
        GxqTtelGetDealEty GxqTtelGetDealEty = gxqTelGetDealService.selectByPkId(pkId);
        //隐藏电话号码 0不隐藏 5隐藏
        if(GxqTtelGetDealEty.getHideTel()==5){
            GxqTtelGetDealEty.setFromTelA(GxqTtelGetDealEty.getFromTelA().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
        }
        SysUser user = ShiroUtils.getSysUser();
        GxqTtelGetDealEty.setYworker(user.getUserName());
        GxqTtelGetDealEty.setYworkNo(user.getUserNo());
        map.put("telGetDeal", GxqTtelGetDealEty);
        map.put("problem", gxqTelGetDealService.problem(GxqTtelGetDealEty.getFromTelA()));
        map.put("history", gxqTelGetDealService.countHistory(GxqTtelGetDealEty.getFromTelA()));
        map.put("noLike", gxqTelGetDealService.noLike(GxqTtelGetDealEty.getFromTelA()));
        map.put("sensitive", sensitiveService.findOneByPkId(GxqTtelGetDealEty.getPkId()));
        return new ModelAndView(prefix + "/process");
    }

    /**
     * 处理页面
     *
     * @param pkId
     * @param map
     * @return
     */
    @RequiresPermissions("mTelRcd:process:view")
    @GetMapping("/telHandle/{pkId}")
    public ModelAndView telHandle(@PathVariable("pkId") Integer pkId, ModelMap map) {
        GxqTtelGetDealEty GxqTtelGetDealEty = gxqTelGetDealService.selectByPkId(pkId);
        SysUser user = ShiroUtils.getSysUser();
        GxqTtelGetDealEty.setYworker(user.getUserName());
        GxqTtelGetDealEty.setYworkNo(user.getUserNo());
        map.put("telGetDeal", GxqTtelGetDealEty);
        map.put("problem", gxqTelGetDealService.problem(GxqTtelGetDealEty.getFromTelA()));
        map.put("history", gxqTelGetDealService.countHistory(GxqTtelGetDealEty.getFromTelA()));
        map.put("noLike", gxqTelGetDealService.noLike(GxqTtelGetDealEty.getFromTelA()));
        map.put("sensitive", sensitiveService.findOneByPkId(GxqTtelGetDealEty.getPkId()));
        return new ModelAndView("system/gaoxin/test/telhandle");
    }

    /**
     * 退回待坐席处理
     *
     * @return
     */
    @RequiresPermissions("system:telPending:view")
    @GetMapping("/telPending")
    public ModelAndView telPending() {
        return new ModelAndView(prefix + "/telPending");
    }


    @RequiresPermissions("system:telHandle:view")
    @GetMapping("/telHandle")
    public ModelAndView telHandle() {
        return new ModelAndView(prefix + "/telHandle");
    }

    @RequiresPermissions("system:telFinish:view")
    @GetMapping("/telFinish")
    public ModelAndView telFinish() {

        return new ModelAndView(prefix + "/telFinish");
    }

    @RequiresPermissions("system:telWork:view")
    @GetMapping("/telWork")
    public ModelAndView telWork() {
        return new ModelAndView(prefix + "/telWork");
    }

    /**
     * 根据id 进行多选删除 和单个删除
     *
     * @param ids
     * @return
     */
    @RequiresPermissions("mTelRcd:remove:view")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String... ids) {
        return toAjax(gxqTelGetDealService.deleteByIds(ids));
    }

    /**
     * 根据 fromTelA 查询来电历史记录
     *
     * @return
     */
    @GetMapping(value = {"/telOver/{fromTelA}", "/telOver"})
    public ModelAndView historyLiaison(@PathVariable(value = "fromTelA", required = false) String fromTelA, ModelMap map) {
        if (fromTelA != null && fromTelA != "") {
            map.put("fromTelA", fromTelA);
        }
        return new ModelAndView(prefix + "/telOver");
    }

    /**
     * 根据fromTealA查询来电历史记录
     *
     * @param
     * @return
     */
    @RequestMapping("/telOver")
    public TableDataInfo historyLiaison(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.historyLiaison(GxqTtelGetDealEty);
        return getDataTable(list);
    }

    /**
     * 根据条件过滤来电历史记录
     *
     * @param GxqTtelGetDealEty 封装查询条件
     * @return 过滤后的来电历史记录列表
     */
    @PostMapping("/telOverFilter")
    public TableDataInfo historyLiaisonFilter(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.historyLiaison(GxqTtelGetDealEty);
//        List<GxqTtelGetDealEty> filteredList = list.stream()
//                .filter(((Predicate<GxqTtelGetDealEty>) curEty -> GxqTtelGetDealEty.getWsCode().equals(curEty.getWsCode()))
//                                .or(curEty -> GxqTtelGetDealEty.getWsKey().equals(curEty.getWsKey()))
//                                .or(curEty -> GxqTtelGetDealEty.getWkFmNo().equals(curEty.getWkFmNo()))
//                                .or(curEty -> curEty.getFmContent().contains(GxqTtelGetDealEty.getFmContent()))
//                )
//                .collect(Collectors.toList());
        return getDataTable(list);
    }

    /**
     * 查询待值班长处理列表页面
     *
     * @return
     */
    @RequiresPermissions("mTelRcd:listMtrWait:view")
    @GetMapping("/listMtrWait")
    public ModelAndView listMtrWaitPage() {
        return new ModelAndView(prefix + "/listMtrWait");
    }

    /**
     * 查询值班长退回列表页面
     *
     * @return
     */
    @RequiresPermissions("mTelRcd:listMtrBack:view")
    @GetMapping("/listMtrBack")
    public ModelAndView listMtrBackPage() {
        return new ModelAndView(prefix + "/listMtrBack");
    }

    /**
     * 查询我办结的来电列表页面
     *
     * @return
     */
    @RequiresPermissions("mTelRcd:listMyOver:view")
    @GetMapping("/listMyOver")
    public ModelAndView listMyOverPage() {
        return new ModelAndView(prefix + "/listMyOver");
    }

    /**
     * 查询待值班长处理列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @RequiresPermissions("mTelRcd:listMtrWait:view")
    @RequestMapping("listMtrWait")
    public TableDataInfo listMtrWait(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        GxqTtelGetDealEty.setLiftWorkNo(user.getUserNo());
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.listMtrWait(GxqTtelGetDealEty);
        return getDataTable(list);
    }

    /**
     * 查询值班长退回列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @RequiresPermissions("mTelRcd:listMtrBack:view")
    @RequestMapping("listMtrBack")
    public TableDataInfo listMtrBack(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        GxqTtelGetDealEty.setLiftWorkNo(user.getUserNo());
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.listMtrBack(GxqTtelGetDealEty);
        return getDataTable(list);
    }

    /**
     * 查询我办结的来电列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @RequiresPermissions("mTelRcd:listMyOver:view")
    @RequestMapping("listMyOver")
    public TableDataInfo listMyOver(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        GxqTtelGetDealEty.setLiftWorkNo(user.getUserNo());
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.listMyOver(GxqTtelGetDealEty);
        return getDataTable(list);
    }


    /**
     * 创建工单 页面
     *
     * @param getDealPk
     * @param map
     * @return
     */
    @GetMapping("/tworkform/{getDealPk}")
    public ModelAndView workform(@PathVariable("getDealPk") Integer getDealPk, ModelMap map) {
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.selectByGetDealPk(getDealPk);
        map.put("workFormBase", GxqTworkFormBaseEty);
        return new ModelAndView("system/gaoxin/test/tworkformadd");
    }

    /**
     * 创建工单
     *
     * @param GxqTworkFormBaseEty
     * @return
     */
    @PostMapping("createWorkform")
    @RepeatSubmit
    public AjaxResult createWorkform(GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        return toAjax(gxqWorkFormBaseService.save(GxqTworkFormBaseEty));
    }

    /**
     * 拟办部门页面
     *
     * @param
     * @param
     * @return
     */
    @GetMapping("/proposed")
    public ModelAndView proposed() {
        return new ModelAndView("system/gaoxin/test/deptdiag");
    }

    /**
     * 拟派工单
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("simulacrum")
    @RepeatSubmit
    public AjaxResult simulacrum(GxqTwfSendBackEty GxqTwfSendBackEty) {
        return toAjax(gxqWfSendBackService.save(GxqTwfSendBackEty));
    }

    @RequestMapping(value = "copy", method = RequestMethod.POST)
    @ResponseBody
    public GxqTtelGetDealEty copy(String id) {
        Integer pkid = Integer.parseInt(id.substring(0));
        return gxqTelGetDealService.selectByPkId(pkid);
    }

    /**
     * 办理情况 页面
     *
     * @param id  派单主键
     * @param map
     * @return
     */
    @GetMapping("/telbackaudit/{id}")
    public ModelAndView telbackaudit(@PathVariable("id") Integer id, ModelMap map) {
        GxqTwfSendBackEty sendBack = gxqWfSendBackService.selectByPkId(id);
        GxqTworkFormBaseEty formBase = gxqWorkFormBaseService.selectByPkId(sendBack.getWfId());
        map.put("formBase", formBase);
        map.put("sendBack", sendBack);
        return new ModelAndView("system/gaoxin/test/telbackaudit");
    }


    /**
     * 随机获取N条待处理来电记录 并保存到中间表中
     */
    @PostMapping("getListWait")
    @RequestBody
    public Object getListWait(Integer num) {
        SysUser user = ShiroUtils.getSysUser();
        List<GxqTtelGetDealEty> list = new ArrayList<>();
        AjaxResult ajaxResult = new AjaxResult();
        try {
            list = gxqTelGetDealService.getListWait(num);
            for (GxqTtelGetDealEty telGetDeal : list) {
                GxqTelGetDealTemp temp = new GxqTelGetDealTemp();
                temp.setTelId(telGetDeal.getPkId());
                temp.setUserNo(user.getUserNo());
                temp.setUserName(user.getUserName());
                temp.setCreateTime(new Date());
                gxqTelGetDealTempService.save(temp);
            }
            ajaxResult.put("msg", "已成功获取到" + String.valueOf(list.size() > num ? num : list.size() + "条数据"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        ajaxResult.put("code", 0);
        return ajaxResult;
    }


    /**
     * 查询电话记录操作日志
     */
    @GetMapping("operationLog/{telId}")
    public ModelAndView operationLog(@PathVariable("telId") Integer telId, ModelMap map) {
        QueryWrapper<GxqTGetDealLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("telId", telId);
        queryWrapper.orderByDesc("logTime");
        List<GxqTGetDealLog> telLogList = gxqTelGetDealLogService.list(queryWrapper);
        map.put("telLogList", telLogList);
        return new ModelAndView("system/mTelRcd/telRecordLog");
    }

    /**
     * 释放获取件 页面
     *
     * @return
     */
    @GetMapping("/freedListWait")
    public ModelAndView freedListWait() {
        return new ModelAndView(prefix + "/freedListWait");
    }

    /**
     * 释放获取件 列表数据
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @PostMapping("freedListWaitJson")
    public TableDataInfo freedListWaitJson(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.freedListWaitJson(GxqTtelGetDealEty);
        return getDataTable(list);
    }

    /**
     * 释放获取件  单个释放和 多选释放
     *
     * @param ids
     * @return
     */
    @PostMapping("removeFreedListWait")
    @RequestBody
    public AjaxResult removeFreedListWait(String... ids) {
        return toAjax(gxqTelGetDealTempService.deleteByIds(ids));
    }

    /**
     * 被诉主题页面
     *
     * @return
     */
    @GetMapping ("/accSubjectDraw")
    public ModelAndView accSubjectDraw(@RequestParam("accSubject")String accSubject, ModelMap map) {
        if (accSubject != null){
            map.put("accSubjectDraw",accSubject);
        }
        return new ModelAndView(prefix + "/accSubjectDraw");
    }

    /**
     * 被诉主题数据
     * @param GxqTtelGetDealEty
     * @return
     */
    @PostMapping("/accSubjectDraw")
    public TableDataInfo accSubjectDraw(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.accSubjectDraw(GxqTtelGetDealEty);
        return getDataTable(list);
    }

    /**
     * 根据fmAddress查询来电历史记录
     *
     * @param
     * @return
     */
    @RequestMapping("/telHistoryByAddress")
    public TableDataInfo telHistoryByAddress(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.telHistoryByAddress(GxqTtelGetDealEty);
        return getDataTable(list);
    }

    @RequestMapping("/relevanceRec")
    public TableDataInfo relevanceRec(GxqTtelGetDealEty GxqTtelGetDealEty) {
        startPage();
        List<GxqTtelGetDealEty> list = gxqTelGetDealService.relevanceRec(GxqTtelGetDealEty);
        return getDataTable(list);
    }
}
